<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<%@ include file="../common/top_css.jsp"%>
<script src="${pageContext.request.contextPath}/bootstrap/js/jquery-1.8.3.js"></script> <!-- jQuery -->
<script type="text/javascript">
	//所有
	var resultList;
	var good=new Array();
	//商品id
	var del=new Array();
	var flagg=true;
	$(function(){	 		
		//校验方法
		function contains(arr, obj) { 
		    var i = arr.length;  
		    while (i--) {  
		        if (arr[i] === obj) {  
		            return true;  
		        }  
		    }  
		    return false;  
		}
	
//商品名
			$.ajax({
				url : "${pageContext.request.contextPath}/goods/selectGoodsList.action",
				type : "POST",
				dataType : "json", 
				data : {},
				success : function(result, start, resp) {
					resultList=result;
					var str='';
					var str2='';
					var str3='';
					var supplierIds=new Array();
					var goodsTypeIds=new Array();
					//生成供应商
					for ( var i = 0; i < result.length; i++) {
						if(!contains(supplierIds,result[i].goodsType.supplier.uuid)){
							supplierIds.push(result[i].goodsType.supplier.uuid);
							str+='<option value="'+result[i].goodsType.supplier.uuid+'">'+result[i].goodsType.supplier.name+'</option>';
						} 
					}
					$("#selectID").append(str);
					
					//生成商品类别
					for ( var i = 0; i < result.length; i++) {
						if(result[i].goodsType.supplier.uuid==supplierIds[0]&&!contains(goodsTypeIds,result[i].goodsType.uuid)){
							str2+='<option value="'+result[i].goodsType.uuid+'">'+result[i].goodsType.name+'</option>';
							goodsTypeIds.push(result[i].goodsType.uuid);
						}
					}
					
					var price='';
					var flag=true;
					//生成商品名称
					for ( var i = 0; i < result.length; i++) {
						if(result[i].goodstypeuuid==goodsTypeIds[0]){
							str3+='<option value="'+result[i].uuid+'">'+result[i].name+'</option>';
							if(flag){
								price=result[i].inpriceView;
								flag=false;
							}
							
						}
					}
					var str1='<tr><td><select class="form-control" name="selectGoodsType">'+
						str2+'</select></td><td><select class="form-control" name="selectGoods">'+
						str3+'</select></td><td><input type="text" style="width:50%;float:left" class="form-control"  name="num"  value="1" /></td><td><input type="text" style="width:50%;float:left" class="form-control" value="'+
						price+'"  name="cpriceId" /></td><td><span  class="tot">'+
						price+'</span>元</td><td><button class="btn btn-xs btn-danger" name="del"><i class="icon-remove"></i></button></td></tr>';
					$("#tdAdd").append(str1);
					$("#totalpriceId").text(price);
				}
			});
			
//商品添加
			$("#addGoods").click(function(){
				$("#selectID").attr("disabled","disabled");
				var supplierid=$("#selectID").val();
				var newList=new Array();
				for ( var i = 0; i < resultList.length; i++) {
					newList.push(resultList[i]);
				}
				var str2='';
				var str3='';
				var price='';
				var flag=true;
				var goodsTypeIds=new Array();
				var goodsID=$("tr:last").children().eq(1).children("select").val();
				if(!contains(good,goodsID)){
					good.push(goodsID);
					$("tr:last").children().eq(0).children("select").attr("disabled","disabled");
					$("tr:last").children().eq(1).children("select").attr("disabled","disabled");
					$("tr:last").children().eq(3).children("input").attr("readonly","readonly");
				}
				for ( var j = 0; j < good.length; j++) {
					for ( var i = 0; i < newList.length; i++) {
						if(newList[i]!=null){
							if(newList[i].uuid==good[j]){
						 		newList[i]=null;
						 	}
					 	}
				 	}
				}

				//商品类别
				for ( var i = 0; i < newList.length; i++) {
					if(newList[i]!=null){
						if(newList[i].goodsType.supplier.uuid==supplierid&&!contains(goodsTypeIds,newList[i].goodsType.uuid)){
							str2+='<option value="'+newList[i].goodsType.uuid+'">'+newList[i].goodsType.name+'</option>';
							goodsTypeIds.push(newList[i].goodsType.uuid);
						}
					}
				}
				//商品名称
				for ( var i = 0; i < newList.length; i++) {
				 	if(newList[i]!=null){
				 		if(newList[i].goodsType.supplier.uuid==supplierid&&newList[i].goodsType.uuid==goodsTypeIds[0]){
				 			str3+='<option value="'+newList[i].uuid+'">'+newList[i].name+'</option>';
				 			if(flag){
								price=newList[i].inpriceView;
								flag=false;
							}
				 		}
				 	}
				}
				
				var str1='<tr><td><select class="form-control" name="selectGoodsType">'+
						str2+'</select></td><td><select class="form-control" name="selectGoods">'+
						str3+'</select></td><td><input type="text" style="width:50%;float:left" class="form-control"  name="num"  value="1" /></td><td><input type="text" style="width:50%;float:left" class="form-control" value="'+
						price+'"  name="cpriceId" /></td><td><span  class="tot">'+
						price+'</span>元</td><td><button class="btn btn-xs btn-danger" name="del"><i class="icon-remove"></i></button></td></tr>';
				if(str2!=''&&str3!=''){
					$("#tdAdd").append(str1);
				}else{
					alert("没有更多商品");
				}

				tot();
			});
			
//供应商change
			$("#selectID").live("change",function(){
				var supplierid=$(this).val();
				var newList=new Array();
				newList=resultList;
				var str2='';
				var str3='';
				var price='';
				var flag=true;
				var goodsTypeIds=new Array();
				for ( var j = 0; j < good.length; j++) {
					for ( var i = 0; i < newList.length; i++) {
						if(newList[i]!=null){
							if(newList[i].uuid==good[j]){
						 		newList[i]=null;
						 	}
					 	}
				 	}
				}
				//商品类别
				for ( var i = 0; i < newList.length; i++) {
					if(newList[i]!=null){
						if(newList[i].goodsType.supplier.uuid==supplierid&&!contains(goodsTypeIds,newList[i].goodsType.uuid)){
							str2+='<option value="'+newList[i].goodsType.uuid+'">'+newList[i].goodsType.name+'</option>';
							goodsTypeIds.push(newList[i].goodsType.uuid);
						}
					}
				}
				//商品名称
				for ( var i = 0; i < newList.length; i++) {
				 	if(newList[i]!=null){
				 		if(newList[i].goodsType.supplier.uuid==supplierid&&newList[i].goodsType.uuid==goodsTypeIds[0]){
				 			str3+='<option value="'+newList[i].uuid+'">'+newList[i].name+'</option>';
				 			if(flag){
								price=newList[i].inpriceView;
								flag=false;
							}
				 		}
				 	}
				}
				
				var str1='<tr><td><select class="form-control" name="selectGoodsType">'+
						str2+'</select></td><td><select class="form-control" name="selectGoods">'+
						str3+'</select></td><td><input type="text" style="width:50%;float:left" class="form-control"  name="num"  value="1" /></td><td><input type="text" style="width:50%;float:left" class="form-control" value="'+
						price+'"  name="cpriceId" /></td><td><span  class="tot">'+
						price+'</span>元</td><td><button class="btn btn-xs btn-danger" name="del"><i class="icon-remove"></i></button></td></tr>';
				if(str2!=''&&str3!=''){
					$("#tdAdd").empty();
					$("#tdAdd").append(str1);
				}
				tot();
			});
			
//商品类型change
			$("select[name='selectGoodsType']").live("change",function(){
				var supplierid=$("#selectID").val();
				var newList=new Array();
				for ( var i = 0; i < resultList.length; i++) {
					newList.push(resultList[i]);
				}
				var goodstypeuuid=$(this).val();
				var str='';
				var price='';
				var flag=true;
				for ( var j = 0; j < good.length; j++) {
					for ( var i = 0; i < newList.length; i++) {
						if(newList[i]!=null){
							if(newList[i].uuid==good[j]){
						 		newList[i]=null;
						 	}
					 	}
				 	}
				}
				for ( var i = 0; i < newList.length; i++) {
					if(newList[i]!=null){
						if(newList[i].goodsType.supplier.uuid==supplierid&&goodstypeuuid==newList[i].goodsType.uuid){
							str+='<option value="'+newList[i].uuid+'">'+newList[i].name+'</option>';
							if(flag){
								price=newList[i].inpriceView;
								flag=false;
							}
						}
					}
				}
				$(this).parent().next().children("select").html(str);
				$(this).parent().next().next().next().children("input").val(price);
				var num=$(this).parent().next().next().children("input").val();
				$(this).parent().next().next().next().next().children("span").html(price*num);
				tot();
			});
//商品change			
			$("select[name='selectGoods']").live("change",function(){
				var goodsuuid=$(this).val();
				var price='';
				for ( var i = 0; i < resultList.length; i++) {
					if(goodsuuid==resultList[i].uuid){
						price=resultList[i].inpriceView;
					}
				}
				$(this).parent().next().next().children("input").val(price);
				var num=$(this).parent().next().children("input").val();
				$(this).parent().next().next().next().children("span").html(price*num);
				tot();
				
			});
			
//计算单行统计
			$("input[name='num']").live("change",function(){
				var num=$(this).val();
				var price=$(this).parent().next().children("input").val();
				$(this).parent().next().next().children("span").html(price*num);
				tot();
			});
//删除
			$("button[name='del']").live("click",function(){
				if($("table > tbody > tr").size()>1){
					flagg=true;
					var val=$(this).parent().parent().children().eq(1).children("select").val();
					good.splice($.inArray(val,good),1);
					$(this).parent().parent().remove();
				}
				tot();
			});
			
			$("#formAdd").click(function (){
				$("select").attr("disabled",false);
				var znum=0;
				$("input[name='num']").each(function(i,dom){
					 znum+=parseInt($(dom).val());
				});
				$("#totleprie").val($("#totalpriceId").html());
				$("#znum").val(znum);
				$("#supplieruuid").val($("#selectID").val());
				$("#formmm").submit();
			
			});


	});
			
 //总合计
	function tot(){
		var total=0;
		$(".tot").each(function(i,dom){
			 total+=parseFloat($(dom).html());
		});
		$("#totalpriceId").html(total);
	}

	
</script>

</head>

<body>
	<div class="navbar navbar-fixed-top bs-docs-nav" role="banner">
		<div class="conjtainer">
  			<%@ include file="../common/top_nav.jsp" %>
		</div>
	</div>

	<%@ include file="../common/top.jsp" %>
	<div class="content">
		<%@ include file="../common/left.jsp" %>
		<div class="mainbar">
		<%@ include file="../common/page-head.jsp" %>
			<div class="matter">
				<div class="container">
				<!-- 内容开始 -->
					<!-- 表单 -->
					<div class="widget wgreen">
		                <div class="widget-head">
		                  <div class="pull-left">搜索</div>
		                  <div class="widget-icons pull-right">
		                    <a href="#" class="wminimize"><i class="icon-chevron-up"></i></a> 
		                    <a href="#" class="wclose"><i class="icon-remove"></i></a>
		                  </div>
		                  <div class="clearfix"></div>
		                </div>
		                <div class="widget-content">
		                  <div class="padd">
		                    <!-- Form starts.  -->
		                     <form class="form-horizontal" role="form">
                                <div class="form-group">
                                  <label class="col-lg-2 control-label">供应商</label>
                                  <div class="col-lg-4">
                                    <select class="form-control" name="supplieruuid" id="selectID">
                                    <!--  <option  value="">--请选择--</option> -->
                                    </select>
                                  </div>
                                  <div class="col-lg-4">
                                    <button type="button"  class="btn btn-info" id="addGoods" >Add</button>
                                    <button type="button"  class="btn btn-info" id="formAdd" >SubMit</button>
                                  </div>
                                </div>   
                                
                              </form>
		                  </div>
		                </div>
		                  <div class="widget-foot">
		                    <!-- Footer goes here -->
		                  </div>
		              </div>	
					<!-- 表格 -->
					<form action="${pageContext.request.contextPath}/order/insertOrder.action" method="post" id="formmm">
					<input type="hidden" name="zprice" id="totleprie">
					<input type="hidden" name="supplieruuid" id="supplieruuid">
					<input type="hidden" name="znum"  id="znum">
					<div class="widget">
		                <div class="widget-head">
		                  <div class="pull-left">商品类别列表</div>
		                  <div class="widget-icons pull-right">
		                    <a href="#" class="wminimize"><i class="icon-chevron-up"></i></a> 
		                    <a href="#" class="wclose"><i class="icon-remove"></i></a>
		                  </div>  
		                  <div class="clearfix"></div>
		                </div>
		                <div class="widget-content">
			                <table class="table table-striped table-bordered table-hover" id="tables">
			                      <thead>
			                        <tr>
			                          <th>类别名称</th>
        			                  <th>商品名称</th>
        			                  <th>采购数量</th>
        			                  <th>单价</th>
        			                  <th>合计</th>
			                          <th>Control</th>
			                        </tr>
			                      </thead>
			                      <tbody id="tdAdd">
			                      </tbody>
			                     	
			                    </table>
			                <div class="widget-foot" >
			                	总计：<span id="totalpriceId"></span>元
			                		
			                </div>
		                    <div class="widget-foot">
		                        <ul class="pagination pull-right">
		                          <li><a href="#">Prev</a></li>
		                          <li><a href="#">1</a></li>
		                          <li><a href="#">2</a></li>
		                          <li><a href="#">3</a></li>
		                          <li><a href="#">4</a></li>
		                          <li><a href="#">Next</a></li>
		                        </ul>
		                      <div class="clearfix"></div> 
		                    </div>
		
		                </div>
		
	                </div>
				
				</form>
				<!-- 内容结束 -->
				</div>
			</div>
		</div>	
		
	});
	</div>
	<%@ include file="../common/footer.jsp" %>
 	<%@ include file="../common/footer_js.jsp" %>
</body>
</html>
